﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BusinessTier
{
    public class TutorReview
    {
        private string teamname;
        private string studentid;
        private string dataConnection = "Data Source=(local);Initial Catalog=INB201;Integrated Security=True";

        public TutorReview(string teamname)
        {
            this.teamname = teamname;
        }


        /// <summary>
        /// Get the name of a student in a given group
        /// </summary>
        /// <returns>String containing group name of group</returns>
        public string getStudentId()
        {
            // test if group name is already stored in memory
            // runs if there isn't
            if (this.studentid != null || this.studentid != "")
            {
                try
                {
                    string studentname = "";
                    SqlConnection connect = new SqlConnection(this.dataConnection);
                    SqlCommand dataCommand = new SqlCommand();
                    dataCommand.Connection = connect;
                    connect.Open();

                    dataCommand.CommandText = "SELECT user_id FROM users WHERE group_name = \'" + this.studentid + "\'";

                    studentname = ((string)dataCommand.ExecuteScalar());
                    this.studentid = studentname;
                    connect.Close();
                }
                catch (Exception e)
                {
                    // label on page is set to exception text
                    return e.ToString();
                }
            }

            // global data member 'group' is returned
            return this.studentid;
        }

        /// <summary>
        /// Retrieves a datatable containing group details of current student
        /// </summary>
        /// <returns>DataTable object containing group details</returns>
        public DataTable groupDetails()
        {
            DataTable dataTable = new DataTable();
            SqlConnection connect = new SqlConnection(this.dataConnection);
            SqlCommand dataCommand = new SqlCommand();
            dataCommand.Connection = connect;
            connect.Open();

            string groupName = this.teamname;

            // Quick check that there isn't a group stored in object memory,
            // if there is no group name saved getGroup() is called
            //if (this.teamname == null || this.group == "")
            //{
            //    groupName = teamname;
            //}
            //else
            //{
            //    groupName = this.teamname;
            //}

            dataCommand.CommandText = "SELECT * FROM users WHERE group_name = '" + groupName + "'";

            // Uses SqlDataReader object to execute command above
            // the data retrieved gets stored in dataTable variable of type DataTable
            SqlDataReader dataReader = dataCommand.ExecuteReader();
            dataTable.Load(dataReader);

            dataReader.Close();
            connect.Close();

            return dataTable;
        }
    }
}
   
